package com.example.jegotrip.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.example.jegotrip.R;
import com.example.jegotrip.MainActivity;
import com.example.jegotrip.base.BaseActivity;
import com.example.jegotrip.db.UserDBHelper;
import com.example.jegotrip.entity.UserInfo;
import com.example.jegotrip.utils.CustomToast;

public class LoginActivity extends BaseActivity {

    private EditText et_username;
    private EditText et_password;

    private Button et_register;
    private boolean is_login;
    private boolean is_checked;
    private SharedPreferences sharedPreferences;

    private CheckBox checkBox;

    /**
     * 监测有没有用户名和密码
     */
    @Override
    protected void initData() {
        //获取SharedPreference实例
        sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);

        is_login = sharedPreferences.getBoolean("is_login", false);
        String username = sharedPreferences.getString("username", "");
        String password = sharedPreferences.getString("password", "");
        if (is_login){
            et_username.setText(username);
            et_password.setText(password);
            checkBox.setChecked(true);
        }
    }

    /**
     * 点击事件
     */
    @Override
    protected void setListener() {
        View login = findViewById(R.id.LoginButton);
        View register = findViewById(R.id.ToSignUpButton);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    CustomToast.showCenterToast(LoginActivity.this, "请输入用户名或密码");
                } else {
                    UserInfo userInfo = UserDBHelper.getInstance(LoginActivity.this).login(username);
                    if (userInfo != null){
                        if (userInfo.getUsername().equals(username) && userInfo.getPassword().equals(password)){
//                            记住密码需要在登录的时候存储用户信息
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("username", username);
                            editor.putString("password", password);
                            editor.putBoolean("is_login", is_login);
                            editor.commit();// 提交
//                            保存用户信息
                            UserInfo.setUserInfo(userInfo);
                            CustomToast.showCenterToast(LoginActivity.this, "登录成功");
                            //回到首页页面
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        } else {
                            CustomToast.showCenterToast(LoginActivity.this, "该账号用户名和密码错误");
                        }

                    } else {
                        CustomToast.showCenterToast(LoginActivity.this, "该账号暂未注册~~");
                    }
                }
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳转到注册页面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                is_login = isChecked;
            }
        });
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_register = findViewById(R.id.ToSignUpButton);
        checkBox = findViewById(R.id.checkbox);
    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.activity_login;
    }
}
